/*
    GASON: SQLmap Wrapper for Burpsuite.
    Copyright (C) 2011-2012  Daniel Garcia (cr0hn) | dani@iniqua.com | twitter: @ggdaniel
    Project page: http://code.google.com/p/gason/

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import CustomPlugin.sqlmapplugin;
import burp.CustomHttpRequestResponse;
import burp.IHttpRequestResponse;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Standalone.java
 *
 * Created on Mar 28, 2012, 2:00:08 PM
 */



/**
 *
 * @author Daniel Garcia Garcia (cr0hn) - dani@iniqua.com
 */
public class Standalone extends javax.swing.JFrame {

    /** Creates new form Standalone */
    public Standalone() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_add = new javax.swing.JTextField();
        btn_less = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lst_urls = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txt_cookie = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txt_post_data = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cmb_method = new javax.swing.JComboBox();
        btn_add1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        btn_sqlmap = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Security tools wrapper v0.9.5 - by Cr0hn (@ggdaniel)");
        setResizable(false);
        getContentPane().setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Targets"));
        jPanel1.setAlignmentX(0.0F);
        jPanel1.setAlignmentY(0.0F);
        jPanel1.setLayout(null);

        jLabel1.setText("Add");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 20, 30, 15);

        jLabel2.setText("URLs");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 40, 40, 15);

        txt_add.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_addKeyReleased(evt);
            }
        });
        jPanel1.add(txt_add);
        txt_add.setBounds(50, 20, 590, 19);

        btn_less.setText("-");
        btn_less.setAlignmentY(0.0F);
        btn_less.setEnabled(false);
        btn_less.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btn_less.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_lessMouseClicked(evt);
            }
        });
        jPanel1.add(btn_less);
        btn_less.setBounds(650, 60, 20, 20);

        lst_urls.setModel(new DefaultListModel());
        lst_urls.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lst_urls);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 60, 630, 120);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Options"));
        jPanel3.setAlignmentX(0.0F);
        jPanel3.setAlignmentY(0.0F);
        jPanel3.setLayout(null);

        jLabel3.setText("Post parameters");
        jPanel3.add(jLabel3);
        jLabel3.setBounds(10, 80, 101, 15);
        jPanel3.add(txt_cookie);
        txt_cookie.setBounds(120, 20, 500, 20);

        jLabel4.setText("Method");
        jPanel3.add(jLabel4);
        jLabel4.setBounds(60, 50, 50, 15);

        txt_post_data.setEnabled(false);
        jPanel3.add(txt_post_data);
        txt_post_data.setBounds(120, 80, 500, 20);

        jLabel5.setText("Cookie");
        jPanel3.add(jLabel5);
        jLabel5.setBounds(70, 20, 44, 15);

        cmb_method.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "GET", "POST" }));
        cmb_method.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmb_methodItemStateChanged(evt);
            }
        });
        jPanel3.add(cmb_method);
        cmb_method.setBounds(120, 50, 60, 20);

        jPanel1.add(jPanel3);
        jPanel3.setBounds(10, 180, 630, 110);

        btn_add1.setText("+");
        btn_add1.setAlignmentY(0.0F);
        btn_add1.setEnabled(false);
        btn_add1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btn_add1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_add1MouseClicked(evt);
            }
        });
        jPanel1.add(btn_add1);
        btn_add1.setBounds(650, 20, 20, 20);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(0, 0, 680, 300);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Send to..."));
        jPanel2.setLayout(null);

        btn_sqlmap.setText("SQLMap");
        btn_sqlmap.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_sqlmapMouseClicked(evt);
            }
        });
        jPanel2.add(btn_sqlmap);
        btn_sqlmap.setBounds(10, 20, 84, 25);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(0, 300, 680, 60);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-692)/2, (screenSize.height-390)/2, 692, 390);
    }// </editor-fold>//GEN-END:initComponents

    private void btn_lessMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_lessMouseClicked
        if(lst_urls.getSelectedIndex() != -1)
        {
            DefaultListModel dm = (DefaultListModel) lst_urls.getModel();
            dm.remove(lst_urls.getSelectedIndex());

            if(dm.getSize() <= 0)
            {
                btn_less.setEnabled(false);
            }
        }
    }//GEN-LAST:event_btn_lessMouseClicked

    private void btn_sqlmapMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_sqlmapMouseClicked
        try {
            String m_url = lst_urls.getSelectedValue().toString();
            String m_post_data = txt_post_data.getText();
            String m_cookie = txt_cookie.getText();
            String m_method = (String)cmb_method.getSelectedItem();

            IHttpRequestResponse m_http = new CustomHttpRequestResponse(m_url, m_cookie, m_post_data, m_method);
            
            sqlmapplugin.getInstance().AddURL(m_http);
        } catch (Exception ex) {
            Logger.getLogger(Standalone.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_sqlmapMouseClicked

    private void btn_add1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_add1MouseClicked
        this._addURL();
    }//GEN-LAST:event_btn_add1MouseClicked

    private void cmb_methodItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmb_methodItemStateChanged
        // TODO add your handling code here:
        if("POST".equals(cmb_method.getSelectedItem().toString()))
        {
            txt_post_data.setEnabled(true);
        }
        else
        {
            txt_post_data.setEditable(false);
        }
    }//GEN-LAST:event_cmb_methodItemStateChanged

    private void txt_addKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_addKeyReleased

        if(evt.getKeyCode() == 10 && !txt_add.getText().isEmpty())
        {
            this._addURL();
        }
        else
        {
            if(txt_add.getText().isEmpty())
            {
                btn_add1.setEnabled(false);
            }
            else
            {
                btn_add1.setEnabled(true);
            }
        }
    }//GEN-LAST:event_txt_addKeyReleased


    private void _addURL()
    {
        if(!txt_add.getText().isEmpty())
        {
            String m_url = txt_add.getText();
            String[] schemes = {"http","https", "ftp"};
            boolean m_validURL = false;
            
            for(int i = 0; i < schemes.length; i++)
            {
                if(m_url.toLowerCase().contains(schemes[i])){
                    m_validURL = true;
                    break;
                }
            }

            if(m_validURL)
            {
                ((DefaultListModel)lst_urls.getModel()).addElement(txt_add.getText());
                txt_add.setText("");
                btn_add1.setEnabled(false);
                btn_less.setEnabled(true);
            }
            else
            {
                JOptionPane.showMessageDialog(this,"You mush specify protocol of URL, like: PROTOCOL://HOST. PE: http://www.google.com", "Invalid URL", JOptionPane.OK_OPTION);
                txt_add.requestFocus();
            }
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_add1;
    private javax.swing.JButton btn_less;
    private javax.swing.JButton btn_sqlmap;
    private javax.swing.JComboBox cmb_method;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lst_urls;
    private javax.swing.JTextField txt_add;
    private javax.swing.JTextField txt_cookie;
    private javax.swing.JTextField txt_post_data;
    // End of variables declaration//GEN-END:variables

}
